package com.ytg.apt;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * @author Administrator 工单详情每一个条目填写的封装
 */
public class CustomCheckBox extends LinearLayout {
    private Context mContext;
    private TextView mTvLeft;// 左边文字
    //    private ImageView mIvbitian;// 必填的标志图标
    ArrayList<JSONObject> list = new ArrayList<>();
LinearLayout mllContainer;
    public CustomCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init(attrs);
    }

    @SuppressLint("NewApi")
    private void init(AttributeSet attrs) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.view_crash_item);
        View layout = inflater.inflate(R.layout.view_checkbox, null);
        layout.setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mTvLeft = (TextView) layout.findViewById(R.id.mTvLeft);
        mllContainer = layout.findViewById(R.id.mllContainer);
        // 设置 左边文字信息
        String leftTextStr = a
                .getString(R.styleable.view_crash_item_setLeftText);
        setLeftText(leftTextStr);
        // 设置 左边文字大小
        int leftTextSize = a.getInt(
                R.styleable.view_crash_item_setLeftTextSize, 14);
        setLeftTextSize(leftTextSize);
        // 设置 左边文字颜色
        int leftTextColor = a.getInt(
                R.styleable.view_crash_item_setLeftTextColor,
                R.color.color_666666);
        setLeftTextColor(leftTextColor);
        LayoutParams params = (LayoutParams) mTvLeft
                .getLayoutParams();
        int LeftTextWidth = a.getInt(R.styleable.view_crash_item_LeftTextWidth, -1);
        if (LeftTextWidth == -1) {
            params.width = LayoutParams.WRAP_CONTENT;
            params.rightMargin = Utils.dip2px(mContext, 6);
        } else {
            params.width = Utils.dip2px(getContext(),
                    LeftTextWidth);
            params.rightMargin = Utils.dip2px(getContext(),
                    a.getInt(
                            R.styleable.view_crash_item_LeftTextMargin, 6));
        }
        mTvLeft.setLayoutParams(params);
//        String data = a.getString(R.styleable.view_crash_item_data);
//        if(!Utils.isEmpty(data)){
//            try {
//                JSONArray array= new JSONArray(data);
//                for (int i = 0; i <array.length() ; i++) {
//                    addRb(array.optJSONObject(i).optString("id"),array.optJSONObject(i).optString("name"),array.optJSONObject(i).optBoolean("checked"));
//                }
//                build();
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//
//        }
        a.recycle();
        this.addView(layout);
    }


    public CustomCheckBox addBox(String id, String name, boolean checked) {
        JSONObject object = new JSONObject();
        try {
            object.put("id", id);
            object.put("name", name);
            object.put("checked", checked);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        list.add(object);
        return this;
    }

    public void removeRb(int pos) {
        if(pos>=list.size()||pos>=mllContainer.getChildCount()){
            try {
                throw new Exception("索引大于总长度"+mllContainer.getChildCount());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
        list.remove(pos);
        mllContainer.removeViewAt(pos);
    }

    public void build() {
        mllContainer.removeAllViews();
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        for (int i = 0, count = list.size(); i < count; i++) {
            CheckBox checkBox = (CheckBox) inflater.inflate(R.layout.view_checkbox_item, null);
            mllContainer.addView(checkBox);
            LayoutParams params = new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.leftMargin = Utils.dip2px(mContext,10);
            checkBox.setLayoutParams(params);
            checkBox.setText(list.get(i).optString("name"));
            checkBox.setTag(getTag());
            checkBox.setChecked(list.get(i).optBoolean("checked"));
            checkBox.setId(i);

        }
    }

//    public void setOnCheckResp(final OnCheckResp listener) {
//        mrg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                for (int i = 0, count = group.getChildCount(); i < count; i++) {
//                    if (group.getChildAt(i).getId() == checkedId) {
//                        listener.back(list.get(i).optString("id"), list.get(i).optString("name"), i, checkedId);
//                        return;
//                    }
//                }
//
//            }
//        });
//    }

    public void disableRadioGroup(RadioGroup testRadioGroup, boolean edit) {
        for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
            testRadioGroup.getChildAt(i).setEnabled(edit);
        }
    }

    public void setLeftImg(int resid) {
        mTvLeft.setCompoundDrawablesWithIntrinsicBounds(getContext().getResources().getDrawable(resid), null, null, null);
        mTvLeft.setCompoundDrawablePadding(Utils.dip2px(getContext(), 6));
        mTvLeft.setGravity(Gravity.CENTER_VERTICAL);
    }

    public void setLeftTextSingle(boolean singleLine) {
        mTvLeft.setSingleLine(singleLine);
    }


    public void setLeftText(String text) {
        mTvLeft.setText(text);
    }

    public String getLeftTextCon() {
        return mTvLeft.getText().toString();
    }

    public void setLeftTextColor(int color) {
        mTvLeft.setTextColor(color);
    }

    public void setLeftTextSize(int size) {
        mTvLeft.setTextSize(size);
    }

    public void setLeftTextMargin(int size) {
        LayoutParams parms = (LayoutParams) mTvLeft.getLayoutParams();
        parms.leftMargin = size;
        mTvLeft.setLayoutParams(parms);
    }

    public void setLeftWrap() {
        LayoutParams params = (LayoutParams) mTvLeft
                .getLayoutParams();
        params.width = LayoutParams.WRAP_CONTENT;
        params.rightMargin = Utils.dip2px(mContext, 10);
        mTvLeft.setLayoutParams(params);
    }

    public interface OnCheckResp {
        void back(String id, String name, int position, int resid);
    }
//    public void setBitianVisible(int Visible) {
//        mIvbitian.setVisibility(Visible);
//        if (Visible == View.GONE) {
//            setLeftTextColor(getResources().getColor(R.color.color_666666));
//        }
//    }
}
